package cn.zhibang.sannuo.commons.utils;

import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.Random;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName SaltUtil.java
 * @Description TODO 生成盐值
 * @createTime 2021年03月30日 19:26:00
 */
public class SaltUtil {

    /**
     * 生成盐值
     * @return  返回盐值
     */
    public static String createSalt(){
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+";
        char[] strChar = str.toCharArray();
        StringBuilder salt = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            salt.append(strChar[new Random().nextInt(str.length())]);
        }
        return salt.toString();
    }

    /**
     * 加密加盐
     * @param password
     * @param saltValue
     * @return
     */
    public static String salt(String password, String saltValue){
        return new SimpleHash("MD5", password, saltValue, 1024).toString();
    }

    /**
     * 生成验证码
     * @return  返回验证码
     */
    public static String createSendOut(){
        String str = "0123456789";
        char[] strChar = str.toCharArray();
        StringBuilder salt = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            salt.append(strChar[new Random().nextInt(str.length())]);
        }
        return salt.toString();
    }

}
